package alipay.openplatform.edu.campus.jiuye.api.controller.business;

import alipay.openplatform.edu.campus.jiuye.aliyun.api.iservice.IAliyunApiService;
import alipay.openplatform.edu.campus.jiuye.api.utils.ContextUtils;
import alipay.openplatform.edu.campus.jiuye.common.enums.ResultCode;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.model.vo.BllLogoInfoVO;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.business.IBllLogoInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/jiuye/api")
public class BllLogoInfoController {

    private static Logger logger = LoggerFactory.getLogger(BllLogoInfoController.class);

    @Autowired
    private IBllLogoInfoService iBllLogoInfoService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IAliyunApiService aliyunApiService;

    /**
     * 添加logo信息
     *
     * @param imgUrl
     * @param logoUrl
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/addLogo")
    @ResponseBody
    public ApiResult addLogo(@RequestParam(value = "imgUrl", required = true) String imgUrl,
                             @RequestParam(value = "logoUrl", required = true) String logoUrl,
                             @RequestParam(value = "startTime", required = true) String startTime,
                             @RequestParam(value = "endTime", required = true) String endTime,
                             @RequestParam(value = "isAd", required = true) Boolean isAd) {
        // 1. 验证参数
        if (ObjectUtils.hasNull(imgUrl, logoUrl, logoUrl, startTime, endTime)) {

            return ApiResult.build("-1", "参数传递不正确!");
        }
        logger.info("添加logo信息|BllLogoInfoController|addLogo|params:imgUrl={},logoUrl={},startTime={},endTime={}", imgUrl, logoUrl, startTime, endTime);
        try {
            //2. 返回结果
            BllLogoInfoVO bllLogoInfoVO = new BllLogoInfoVO();
            bllLogoInfoVO.setImgUrl(imgUrl);
            bllLogoInfoVO.setLogoUrl(logoUrl);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            bllLogoInfoVO.setStarttime(startDate);
            bllLogoInfoVO.setEndtime(endDate);
            bllLogoInfoVO.setAd(isAd);
            String userId = ContextUtils.getUserId(request);
            logger.info("BllLogoInfoController|addLogo|从Session中获取的userId={}", userId);
            bllLogoInfoVO.setCreatemanid(Integer.parseInt(userId));
            iBllLogoInfoService.addLogo(bllLogoInfoVO);
        } catch (Exception e) {
            logger.info("添加logo信息|BllLogoInfoController|addLogo|params:imgUrl={},logoUrl={},startTime={},endTime={},message={}", imgUrl, logoUrl, startTime, endTime, e);
            return ApiResult.build(ResultCode.ADD_LOGO_FAIL.getCode(), ResultCode.ADD_LOGO_FAIL.getName());
        }
        return ApiResult.build(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName());
    }

    /**
     * 根据id号删除logo信息
     *
     * @param logoId
     * @return
     */
    @RequestMapping("/delLogo")
    @ResponseBody
    public ApiResult delLogo(@RequestParam(value = "logoId", required = true) Integer logoId) {
        // 1. 验证参数
        if (ObjectUtils.hasNull(logoId)) {

            return ApiResult.build("-1", "参数传递不正确!");
        }
        logger.info("根据id号删除logo信息|BllLogoInfoController|delLogo|params:logoId={}", logoId);
        try {
            //2. 返回结果
            iBllLogoInfoService.delLogo(logoId);
        } catch (Exception e) {
            logger.info("根据id号删除logo信息|BllLogoInfoController|delLogo|params:logoId={},message={}", logoId, e);
            return ApiResult.build(ResultCode.DEL_LOGO_FAIL.getCode(), ResultCode.DEL_LOGO_FAIL.getName());
        }
        return ApiResult.build(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName());
    }

    /**
     * 获取当前的运营位列表
     *
     * @return
     */
    @RequestMapping("/getLogoList")
    @ResponseBody
    public ApiResult getLogoList() {
        logger.info("获取当前的运营位列表|BllLogoInfoController|getLogoList|params:NO");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<BllLogoInfoVO> bllLogoInfoVOList = new ArrayList<BllLogoInfoVO>();
            bllLogoInfoVOList = iBllLogoInfoService.queryByIsAd(Boolean.FALSE);
            if (bllLogoInfoVOList!=null && bllLogoInfoVOList.size()>0) {
                result.put("list", bllLogoInfoVOList);
            }else {
                return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), null);
            }
        } catch (Exception e) {
            logger.error("获取当前的运营位列表|BllLogoInfoController|getLogoList|params:message={}", e);
            return ApiResult.build(ResultCode.GET_CURRENT_LOGO_INFO.getCode(), ResultCode.GET_CURRENT_LOGO_INFO.getName());
        }
        return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), result);
    }

    /**
     * 获取广告位列表
     *
     * @return
     */
    @RequestMapping("/getAdList")
    @ResponseBody
    public ApiResult getAdList() {
        logger.info("获取广告位列表|BllLogoInfoController|getAdList|params:NO");
        Map<String, Object> result = new HashMap<String, Object>();
        try {

            List<BllLogoInfoVO> bllLogoInfoVOList = new ArrayList<BllLogoInfoVO>();
            bllLogoInfoVOList = iBllLogoInfoService.queryByIsAd(Boolean.TRUE);
            if (bllLogoInfoVOList!=null && bllLogoInfoVOList.size()>0) {
                result.put("list", bllLogoInfoVOList);
            }else {
                return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), null);
            }
        } catch (Exception e) {
            logger.error("获取广告位列表|BllLogoInfoController|getAdList|params:message={}", e);
            return ApiResult.build(ResultCode.GET_AD_INFO.getCode(), ResultCode.GET_AD_INFO.getName());
        }
        return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), result);
    }

    /**
     * 获取运营位信息-后台
     *
     * @return
     */
    @RequestMapping("/queryAllPictures")
    @ResponseBody
    public ApiResult queryAllPictures() {
        logger.info("获取运营位信息-后台|BllLogoInfoController|queryAllPictures|params:NO");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<BllLogoInfoVO> bllLogoInfoVOList = iBllLogoInfoService.queryAllAds();
            result.put("list", bllLogoInfoVOList);
        } catch (Exception e) {
            logger.error("获取广告位列表|BllLogoInfoController|queryAllPictures|params:message={}", e);
            return ApiResult.build(ResultCode.GET_LOGO_INFO.getCode(), ResultCode.GET_LOGO_INFO.getName());
        }
        return ApiResult.buildSuccess(ResultCode.STATUS_OK.getCode(), ResultCode.STATUS_OK.getName(), result);
    }

    /**
     * 请用contentType=text/plan
     *
     * @return
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult uploadImage(InputStream inputStream) {
        try {
            String fileUrl = aliyunApiService.transferInputStreamToAliyunOSS(inputStream);
            return ApiResult.buildSuccess("0", "图片上传成功", fileUrl);
        } catch (IOException e) {
            logger.error("上传图片|BllLogoInfoController|uploadImage|params:message{}", "", e);
            return ApiResult.build("14", "图片上传失败");
        }
    }

}